package com.cold.demo.generator.mybatis;

import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zyc
 * @Date Created in 2019/9/4 17:49
 * @Description
 */
public class GeneratorCreator {


    public Configuration createCode(String xmlPath) throws Exception {
        String               file             = xmlPath;
        List<String>         warnings         = new ArrayList<>();
        boolean              overwrite        = true;
        File                 configFile       = new File(file);
        ConfigurationParser  cp               = new ConfigurationParser(warnings);
        Configuration        config           = cp.parseConfiguration(configFile);
        DefaultShellCallback callback         = new DefaultShellCallback(overwrite);
        MyBatisGenerator     myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
        //生成代码
        myBatisGenerator.generate(null);
        return config;
    }
}
